package cn.yshujia.service.impl;

import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.entity.Classify;
import cn.yshujia.domain.entity.Label;
import cn.yshujia.domain.vo.ClassifyLabelVO;
import cn.yshujia.domain.vo.LabelVO;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.mapper.LabelMapper;
import cn.yshujia.repository.ClassifyRepository;
import cn.yshujia.repository.LabelRepository;
import cn.yshujia.transform.ClassifyTransform;
import cn.yshujia.utils.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 服务层
 */

@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements IService<Label> {
	
	@Resource
	public LabelMapper mapper;
	
	@Resource
	public LabelRepository repository;
	
	@Resource
	ClassifyServiceImpl classifyService;
	
	public LabelVO selectById(Long id) {
		return repository.get(id);
	}
	
	public List<LabelVO> selectListByClassifyId(Long id) {
		return repository.selectListByClassifyId(id);
	}
	
	public List<LabelVO> selectListByClassifyType(int type) {
		return repository.selectListByClassifyType(type);
	}
	
	@Resource
	public ClassifyRepository classifyRepository;
	
	
	public List<ClassifyLabelVO> getClassifyTreeByType(Integer type) {
		List<Classify> list = classifyRepository.selectListByType(type);
		if (ObjectUtils.isEmpty(list)) {
			return new ArrayList<>();
		}
		List<ClassifyLabelVO> voList = new ArrayList<>(list.size());
		for (Classify classifyVO : list) {
			ClassifyLabelVO vo = ClassifyTransform.vo2VO(classifyVO);
			vo.setLabels(repository.selectListByClassifyId(classifyVO.getId()));
			voList.add(vo);
		}
		return voList;
	}
	
	public ClassifyLabelVO getClassifyTreeById(Long id) {
		Classify classifyVO = classifyRepository.selectById(id);
		if (null == classifyVO) {
			return new ClassifyLabelVO();
		}
		ClassifyLabelVO vo = ClassifyTransform.vo2VO(classifyVO);
		vo.setLabels(repository.selectListByClassifyId(classifyVO.getId()));
		return vo;
	}
	
	public PageVO<LabelVO> adminPage(PageDTO dto) {
		QueryWrapper<Label> wrapper = PageUtils.createLabelWrapper(dto);
		IPage<Label> page = mapper.selectPage(new Page<>(dto.getCurrent(), dto.getSize()), wrapper);
		// 创建VO集合
		if (!ObjectUtils.isEmpty(page) && !ObjectUtils.isEmpty(page.getRecords())) {
			return PageUtils.getPage(dto, repository.createListVO(page.getRecords()), page.getTotal());
		}
		return new PageVO<>();
	}
	
	@Transactional (rollbackFor = {Exception.class})
	public Boolean insert(Label label) {
		List<LabelVO> list = repository.selectListByClassifyId(label.getClassifyId());
		if (!ObjectUtils.isEmpty(list)) {
			label.setId(list.get(0).getId() + 1);
		} else {
			label.setId(null);
		}
		int n = mapper.insert(label);
		if (n > 0) {
			repository.del();
		}
		return n > 0;
	}
	
	@Transactional (rollbackFor = {Exception.class})
	public Boolean update(Label label) {
		int n = mapper.updateById(label);
		if (n > 0) {
			repository.del();
		}
		return n > 0;
	}
	
	@Transactional (rollbackFor = {Exception.class})
	public Boolean remove(List<Long> ids) {
		int n = mapper.deleteByIds(ids);
		if (n > 0) {
			repository.del();
		}
		return n > 0;
	}
}
